package bt.basics.common.utils;

import java.net.URLEncoder;
import java.util.Map;

/**
 * url参数拼接
 */
public class UrlUtils {
    public static String appendUrl(String url, Map<String, Object> data) {
        String newUrl = url;
        StringBuffer param = new StringBuffer();
        for (String key : data.keySet()) {
            //字符编码异常处理
            try {
                param.append(key + "=" + URLEncoder.encode(data.get(key).toString(),"UTF-8") + "&");
            }catch (Exception ex){
                ex.printStackTrace();
            }
        }
        String paramStr = param.toString();
        paramStr = paramStr.substring(0, paramStr.length() - 1);
        if (newUrl.indexOf("?") >= 0) {
            newUrl += "&" + paramStr;
        } else {
            newUrl += "?" + paramStr;
        }
        return newUrl;
    }

}
